/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Salesman;

import Salesman.Data.DataProvider;
import java.util.AbstractQueue;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;

/**
 *
 * @author weinpau
 */
public class Queue extends AbstractQueue<Distances> {

    private PriorityQueue<DistancesNode> nodes = new PriorityQueue<DistancesNode>();

    @Override
    public Iterator<Distances> iterator() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int size() {
        return nodes.size();
    }

    @Override
    public boolean offer(Distances e) {
        return nodes.add(new DistancesNode(e.getRoute(), e.getUnused(), e.minRemaining()));
    }

    @Override
    public Distances poll() {
        return nodes.poll().toDistances();
    }

    @Override
    public Distances peek() {
        return nodes.peek().toDistances();
    }
}

class DistancesNode implements Comparable<DistancesNode> {

    private List<Integer> route;
    private List<Integer> unused;
    private double minRemaining;

    public DistancesNode(List<Integer> route, List<Integer> unused, double minRemaining) {
        this.route = route;
        this.unused = unused;
        this.minRemaining = minRemaining;
    }

    public double minRemaining() {
        return minRemaining;
    }

    public List<Integer> getRoute() {
        return route;
    }

    public Distances toDistances() {
        Distances d = DataProvider.getDistances();
        for (int r : getRoute()) {
            d = d.visit(r);
        }
        d.setUnused(unused);
        
        return d;
    }

    @Override
    public int compareTo(DistancesNode o) {
        return Double.compare(minRemaining(), o.minRemaining());
    }
}
